package com.example.safeassistan.train.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.train.activity.ActivityKnowledgeBaseDetail;
import com.example.safeassistan.train.adapter.KonwLedgeBaseAdapter;
import com.example.safeassistan.train.model.CloudCategory;
import com.example.safeassistan.train.model.MateralList;
import com.example.safeassistan.utile.JsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MaterialFragment extends BaseVPFragment implements View.OnClickListener{
    private KonwLedgeBaseAdapter materialAdapter;
    private RecyclerView rcView;
    private LinearLayout llNodata;
    private View view;
    private Context context;
    private List<MateralList.ResultDTO> data;

    private String categoryId="";


    public static MaterialFragment newInstance(String categoryId) {
        Bundle arguments = new Bundle();
        arguments.putString("categoryId",categoryId);
        MaterialFragment fl = new MaterialFragment();
        fl.setArguments(arguments);
        return fl;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_material, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {
            getData();
    }

    //获取数据
    public void getData(){
        Map<String, String> map = new HashMap<>();
        map.put("sectionId",String.valueOf(categoryId));
        new HttpRequest(context).doGet(UrlConstants.taskqueryRepository, null, map, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), MateralList.class);
                        if (obj instanceof MateralList) {
                            MateralList  model=(MateralList)obj;
                            data.clear();
                            if (model.getResult().size() > 0) {
                                llNodata.setVisibility(View.GONE);
                                rcView.setVisibility(View.VISIBLE);
                                data=model.getResult();
                                materialAdapter=new KonwLedgeBaseAdapter(data);
                                rcView.setLayoutManager(new LinearLayoutManager(context));
                                rcView.setAdapter(materialAdapter);
                                materialAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        ActivityKnowledgeBaseDetail.actionStart(context,
                                                data.get(position).getId());
                                    }
                                });
                            } else {
                                rcView.setVisibility(View.GONE);
                                llNodata.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void initView(){
        categoryId=getArguments().getString("categoryId");
        rcView=view.findViewById(R.id.rcView);
        llNodata=view.findViewById(R.id.llNodata);
        data=new ArrayList<>();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            default:
                break;
        }
    }
}
